/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.domain.action;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.business.customer.core.domain.Customer;
import info.peigen.hotpot.business.customer.facade.enums.auth.CustomerCertStatus;

import static info.peigen.hotpot.business.customer.facade.enums.CustomerStatus.*;


/**
 * <b>(CustomerCheck)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/5/23
 */
public class CustomerCheck {
    public static void activeCheck(Customer customer) {
        customer.check();
        Assert.isTrue(!customer.getPermission().permissionAllClose(), "该客户权限全无");
    }

    public static void registerCheck(Customer customer) {
        customer.check();
        customer.getAuthentication().check();
        customer.getCustomerBase().check();
        customer.getMasterOperator().check();
        operatorsNotEmptyCheck(customer);
        customer.getPermission().check();
        customer.getCustomerMarketing().check();
        Assert.isTrue(customer.getStatus().isInList(Un_Cert), "客户已注册!");
    }

    public static void quickRegisterCheck(Customer customer) {
        customer.check();
        customer.getAuthentication().check();
        customer.getCustomerBase().check();
        customer.getPermission().check();
        customer.getCustomerMarketing().check();
        Assert.isTrue(customer.getStatus().isInList(Quick_Register), "客户已注册!");
    }

    public static void loginCheck(Customer customer) {
        if (ObjectUtil.isNull(customer)) {
            throw new IllegalArgumentException("客户名或密码错误!");
        }
        customer.check();
        Assert.isTrue(!customer.getStatus().isInList(Un_Activated, Init, UNKNOWN, Freeze, Un_Subscribe), "客户状态不允许登录!");
    }

    public static void forgetPasswordCheck(Customer customer) {
        if (ObjectUtil.isNull(customer)) {
            throw new IllegalArgumentException("客户不存在!");
        }
        customer.check();
        Assert.isTrue(!customer.getStatus().isInList(Un_Activated, Init, UNKNOWN, Freeze, Un_Subscribe), "客户状态不允许登录!");
    }

    public static void applyAuthenticationCheck(Customer customer) {
        customer.check();
        Assert.isTrue(customer.getAuthentication().getCustomerCertStatus().isInList(CustomerCertStatus.CERT_INIT, CustomerCertStatus.CERT_REFUSE, CustomerCertStatus.CERT_PAST, CustomerCertStatus.CERT_PASTING), "客户当前无法实名认证!");
    }

    public static void auditAuthenticationCheck(Customer customer) {
        customer.check();
        Assert.isTrue(customer.getAuthentication().getCustomerCertStatus().isInList(CustomerCertStatus.CERT_AUDITING), "客户未提交实名认证信息!");
    }

    private static void operatorsNotEmptyCheck(Customer customer) {
        Assert.isTrue(CollectionUtil.isNotEmpty(customer.getOperators()));
    }
}